﻿using DXApplicationTangche.service;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.标签列印
{
    public partial class Frm美国坚山水洗标打印 : DevExpress.XtraEditors.XtraForm
    {
        private String barCode;
        private String orderNo;
        private String customerName;
        private String fabricNumber;
        private String fabricComposition;
        public Frm美国坚山水洗标打印()
        {
            InitializeComponent();
            this.ClearTextBox();
        }

        private void textBox条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                try
                {
                    DataRow dr = FabricService.Get美国坚山水洗数据(this.textBox条码.Text.Trim());
                    this.textBox订单号.Text = dr["ORDER_NO"].ToString();
                    this.textBox客户名称.Text = dr["CUSTOM_NAME"].ToString();
                    this.textBox面料号.Text = dr["MATERIAL_CODE"].ToString();
                    this.textBox面料成分.Text = dr["MATERIAL_COMPOSITION"].ToString();
                    this.print();
                }
                catch { }
            }
        }

        private void print()
        {
            this.barCode = this.textBox条码.Text.Trim();
            this.orderNo = this.textBox订单号.Text.Trim();
            this.customerName = this.textBox客户名称.Text.Trim();
            this.fabricNumber = this.textBox面料号.Text.Trim();
            this.fabricComposition = this.textBox面料成分.Text.Trim();
            Engine btEngine = new Engine();
            btEngine.Start();
            //string lj = AppDomain.CurrentDomain.BaseDirectory + "顺丰订单模板.btw";  //test.btw是BT的模板
            //string lj = AppDomain.CurrentDomain.BaseDirectory + "001.btw";  //test.btw是BT的模板
            String lj = "C:\\美国坚山水洗标模板.btw";
            LabelFormatDocument btFormat = btEngine.Documents.Open(lj);
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            btFormat.PrintSetup.PrinterName = "TEC B-462-R";
            //btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 
            btFormat.SubStrings["barCode"].Value = this.barCode;
            btFormat.SubStrings["orderNo"].Value = this.orderNo;
            btFormat.SubStrings["customerName"].Value = this.customerName;
            btFormat.SubStrings["fabricNumber"].Value = this.fabricNumber;
            btFormat.SubStrings["fabricComposition"].Value = this.fabricComposition;
            try
            {
                btFormat.SubStrings["RN"].Value = "RN# " + this.barCode;
            }
            catch { }
            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Result nResult1 = btFormat.Print("美国坚山水洗标" + this.orderNo, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }

        private void ClearTextBox()
        {
            this.barCode = "";
            this.orderNo = "";
            this.customerName = "";
            this.fabricNumber = "";
            this.fabricComposition = "";
            this.textBox条码.Clear();
            this.textBox订单号.Clear();
            this.textBox客户名称.Clear();
            this.textBox面料号.Clear();
            this.textBox面料成分.Clear();
        }
        private void simpleButton打印_Click(object sender, EventArgs e)
        {
            this.print();
        }

        private void simpleButton取消_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
